package com.wuyan.locationdog.ui.device

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.arcsoft.commonbase.base.BaseBindingActivity
import com.arcsoft.commonbase.bean.DogFileBeanContent
import com.arcsoft.commonbase.bus.Bus
import com.arcsoft.commonbase.bus.bus_device_select
import com.arcsoft.commonbase.bus.bus_fence_refresh
import com.arcsoft.commonbase.ext.createEmptyView
import com.arcsoft.commonbase.ext.customShowLong
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ToastUtils
import com.wuyan.locationdog.R
import com.wuyan.locationdog.adapter.DeviceList2Adapter
import com.wuyan.locationdog.databinding.MyDeviceSelectListActivityBinding
import com.wuyan.locationdog.help.DeviceHelp
import com.wuyan.locationdog.help.DogHelp
import com.wuyan.locationdog.viewmodel.DeviceViewModel


class MyDeviceSelectListActivity : BaseBindingActivity<MyDeviceSelectListActivityBinding>() {
    private var dogList: MutableList<DogFileBeanContent>? = null
    private var isAreaModify: Boolean = false
    private val mViewModel: DeviceViewModel by viewModels()

    private val mAdapter by lazy { DeviceList2Adapter(R.layout.item_device_select_list_feedback, null) }

    override fun initData() {
        setTitleText("选择设备", rightTitle = "全选") {
            mAdapter.data.forEach {
                it.isSelect = true
            }
            mAdapter.notifyDataSetChanged()

        }


        binding.apply {
            val map = DeviceHelp.areaMap
            isAreaModify = map.containsKey("areaId")
            if (isAreaModify) {
                val areaId = map.get("areaId").toString()
                val map = HashMap<String, Any>()
                map["areaId"] = areaId
                mViewModel.getAreaDogList(map)
            }
            btnSure.setOnClickListener {
                if (mAdapter.data.find { it.isSelect } == null) {
                    customShowLong("请选择设备")
                    return@setOnClickListener
                }
                map["deviceIdList"] = mAdapter.data.filter { it.isSelect }.map { it.deviceId }
                if (isAreaModify) {
                    mViewModel.updateArea(map)
                } else {
                    mViewModel.addArea(map)
                }
            }

            btnLast.setOnClickListener { finish() }

            recyclerView.apply {
                layoutManager = LinearLayoutManager(context)
                adapter = mAdapter.apply {
                    setEmptyView(createEmptyView("暂无数据"))

                    setOnItemClickListener { adapter, view, position ->
                        data[position].isSelect = !data[position].isSelect
                        notifyDataSetChanged()
                        btnSure.isEnabled = true
                    }
                }
            }


        }
    }

    override fun loadNetData() {
        super.loadNetData()
        val map = HashMap<String, Any>()
        map["pageSize"] = 9999
        map["pageNum"] = 1
        mViewModel.deviceList(map)
    }

    override fun observe() {
        super.observe()
        mViewModel.deviceListBean.observe(this) {
            mAdapter.setNewInstance(it.content?.toMutableList())
            if (isAreaModify) {
                if (!dogList.isNullOrEmpty()) {
                    dogList?.filter { it.ifBindArea }?.forEachIndexed { index, dogFileBeanContent ->
                        mAdapter.data.find { it.deviceId == dogFileBeanContent.deviceId}?.isSelect = true
                    }
                    mAdapter.notifyDataSetChanged()
                }
            }
        }

        mViewModel.mDogFileBean.observe(this) {
            dogList = it.content.toMutableList()
            if (isAreaModify) {
                if (!mAdapter.data.isNullOrEmpty()) {
                    dogList?.filter { it.ifBindArea }?.forEachIndexed { index, dogFileBeanContent ->
                        mAdapter.data.find { it.deviceId == dogFileBeanContent.deviceId}?.isSelect = true
                    }
                    mAdapter.notifyDataSetChanged()
                }
            }
        }
        mViewModel.anyBean.observe(this) {
            Bus.post(bus_fence_refresh, "")
            ActivityUtils.finishToActivity(DeviceFenceListActivity::class.java, false)
        }
    }

}